#include <iostream>           // std::cout
#include <thread>             // std::thread, std::this_thread::yield
#include <mutex>              // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable


std::mutex mtx;
std::condition_variable cv;
int cargo=0;

bool  thread_available(){
    return cargo !=0;
}
void consume (int n) 
{
    for (int i=0; i<n; ++i) 
    {
        std::unique_lock<std::mutex> lck(mtx);

        cv.wait(lck,thread_available);// consume:
        std::cout << cargo << '\n';
        cargo=0;
    }
}

int main(){
    std::thread  thread1(consume,10);
    for ( int i = 0; i < 10; i++)
    {
        while (thread_available())  std::this_thread::yield;
        std::unique_lock<std::mutex> lck(mtx);
        cargo = i+1;
        cv.notify_one();
    }
    thread1.join();
    return 0;
}
